package com.margo.project.web.mvcconfig.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.margo.project.web.mvcconfig.model.MargoInitbinderVO;
import com.margo.project.web.utils.ResultUtils;
import com.margo.project.web.utils.ResultVO;

/**
 * https://segmentfault.com/a/1190000019822822?utm_source=tag-newest
 * 1) @InitBinder 针对表单提交
 * @author fangxingbang
 *
 */
@Controller
@RequestMapping("/initbinder")
public class MargoInitbinderController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@PostMapping("/form")
	@ResponseBody
	public ResultVO queryForm(MargoInitbinderVO margoInitbinderVO) {
		logger.info(margoInitbinderVO.toString());
		return ResultUtils.success(1);
	}
	
	@PostMapping("/json")
	@ResponseBody
	public ResultVO queryJson(@RequestBody MargoInitbinderVO margoInitbinderVO) {
		logger.info(margoInitbinderVO.toString());
		return ResultUtils.success(1);
	}
	
	@PostMapping("/converters")
	@ResponseBody
	public ResultVO converters(Date date) {
		logger.info(date.toString());
		return ResultUtils.success(1);
	}

	//@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}
